﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StreamingServer
{
    public partial class frm_Server : Form
    {
        short port = 554;
        string path= System.Environment.CurrentDirectory + "\\VideoFiles\\";
        int success = 0;//启动成功标志
        public frm_Server()
        {
            InitializeComponent();
        }

        private void frm_Server_Load(object sender, EventArgs e)
        {
            if (HikServer.InitStreamServerLib() == 0)
            {                
                if (HikServer.StartServer(path, port) == 0)//启动服务成功
                {
                    btnStart.Enabled = false;
                    success = 1;
                    this.timerHikServer.Start();
                    this.timer1.Start();
                    MessageBox.Show("服务器启动成功");
                }
                else MessageBox.Show("服务未启动");
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (success != 1)
            {
                HikServer.StartServer(path, port);
                HikServer.RunServer();//示例代码中使用了。
                btnStart.Enabled = false;
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (success == 0)
            {
                return;
            }
            if (HikServer.StopServer() == 0)
            {
                MessageBox.Show("服务停止成功");
                success = 0;                
                timer1.Stop();
                btnStart.Enabled = true;
            }
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            if (success != 0)
            {
                MessageBox.Show("请先停止服务");
                return;
            }
            if (HikServer.FiniStreamServerLib() == 0)//关闭ACE
            {
                //关闭ACE
            }
        }

        private void timerHikServer_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            label5.Text = dt.ToLongTimeString();
            label6.Text = dt.ToShortDateString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Interval = 60000;// 把timer2.Interval = 60000;//1分钟
            //获取当前连接的设备通道数
            label3.Text = Convert.ToString(HikServer.GetCurChannel());
            //获取当前连接的客户端数目
            label4.Text = Convert.ToString(HikServer.GetCurConnection());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            frm_Setting frmset = new frm_Setting();
            frmset.ShowDialog();
        }
        /*timerc#objectstringTimer控件

            Timer控件主要会用到2个属性一个是Enabled和Interval
            Enabled主要是控制当前Timer控件是否可用
            timer1.Enabled=false;不可用
            timer1.Enabled=true;可用
            timer1.Interval=1000;主要是设置timer2_Tick事件的时间，单位为毫秒

            例一：到9:00提示去上厕所：（
            把timer2.Interval=60000;//1分钟
         
            private void timer2_Tick(object sender, System.EventArgs e)
           {
                    string cesuotime=DateTime.Now.DateTime.Now.ToShortTimeString();//得到现在的时间
                     if(cesuotime.equles("9:00")){
                                   timer1.Enabled=false;
                                   MessageBox.show("该去上厕所了");
                                   timer1.Enabled=true;//如果不先把enabled设置成false对话框会一直弹下去
                       }
            }
        例二：每2小时提示用户看电脑时间已经很久了，需要休息了
        把timer2.Interval=7200000;//7200秒


        private void timer2_Tick(object sender, System.EventArgs e){
                                timer1.Enabled=false;
                               MessageBox.show("需要休息了，开机已经2小时了");
                               timer1.Enabled=true;//如果不先把enabled设置成false对话框会一直弹下去
           }
        }
         */
        #region 该回调函数不知道什么意思，如何使用
        public int CBF_NotifyANewConnectioncallback(int badd, string clientip, short port, string url)
        {
            HikServer.callbackinfo pinfo = new HikServer.callbackinfo();
            memset(clientip, port,badd, 1024);
            if (url != "")
            {
                strcpy(url);
            }
            return 0;
        }

        private void strcpy(string url)
        {
            throw new NotImplementedException();
        }

        private void memset(string clientip,short port, int badd, ulong size)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
